﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Dln.Common;
using Dln.Common.Helper;
using Dln.Handler;
using Dln.Model;

public partial class Page_AutoUpdate_WeChatStatics : System.Web.UI.Page
{
    protected string CurrInHospNum = string.Empty; //入院
    protected string CurrOutHospNum = string.Empty; //出院
    protected string CurrLeaveHospNum = string.Empty; //退院
    protected string CurrInHospNums = string.Empty; //在院
    protected string CurrCriticallyIllInHospNum = string.Empty; //病危
    protected string YesterdayAmount = string.Empty; //昨日业务收入
    protected string YesterdayOutNum = string.Empty; //昨日门诊人次
    protected string Key = string.Empty;
    protected string UnicodeKey = string.Empty;
    protected bool YesterdayAmountquanxian = false; //业务收入权限
    protected bool CurrInHospNumquanxian = false; //入院权限
    protected bool CurrOutHospNumquanxian = false; //出院权限
    protected bool CurrLeaveHospNumquanxian = false; //退院权限
    protected bool CurrInHospNumsquanxian = false; //在院权限
    protected bool CurrCriticallyIllInHospNumsquanxian = false; //病危权限
    protected bool YesterdayOutNumquanxian = false; //昨日门诊人次
    protected string a = "123";


    protected string[] activityRebateType = null;
    protected DateTime ParmeDt = DateTime.Now.Date;
    protected void Page_Load(object sender, EventArgs e)
    {

        var uptModel = SqlHelper.Context.From<WeChatUsers>().Where(d => d.ISSendWeChat1 == 1).ToList();
        if (uptModel != null && uptModel.Count > 0)
        {
            foreach (var item in uptModel)
            {
                
            }
        }


        try
        {
            var searchDate = HttpContext.Current.Request.QueryString["NowDateTime"];
            if (!string.IsNullOrEmpty(searchDate))
            {
                ParmeDt = Convert.ToDateTime(searchDate).Date;
            }
            var openid = GetAuthOpenId();
            var weChatUsers = SqlHelper.Context.From<WeChatUsers>().Where(d => d.Openid == openid).ToFirstDefault();
            if (weChatUsers != null)
            {
                var type = weChatUsers.PositionRoles;
                var positionModel = SqlHelper.Context.From<PositionTypes>().Where(d => d.Id == type).ToFirstDefault();
                if (positionModel != null)
                {
                    activityRebateType = positionModel.Interfaces.TrimEnd(',').Split(',');
                }
            }

            Key = "Laf5JVhF/H36iMGyB9dXpZq2j95klaqOaFlPqz4VwHuaA6oxp6JYEw==";
            UnicodeKey = Key.Replace("+", "%2B");

            //昨日入院人数

            var getCurrInHospNum = SqlHelper.Context.From<WebServiceData>().Select(m => m.ServiceData)
                    .Where(m => m.ServiceName == "GetCurrDayInHospNum" && m.SearchDate == ParmeDt.AddDays(-1)).ToFirstDefault();
            if (getCurrInHospNum != null)
            {
                CurrInHospNum = getCurrInHospNum.ServiceData;
                a = CurrInHospNum;
            }

            //昨日出院人数
            var getCurrOutHospNum = SqlHelper.Context.From<WebServiceData>().Select(m => m.ServiceData)
                   .Where(m => m.ServiceName == "GetCurrOutHospNum" && m.SearchDate == ParmeDt.AddDays(-1)).ToFirstDefault();
            if (getCurrOutHospNum != null)
            {
                CurrOutHospNum = getCurrOutHospNum.ServiceData;
            }

            //昨日退院人数
            var getCurrLeaveHospNum = SqlHelper.Context.From<WebServiceData>().Select(m => m.ServiceData)
                   .Where(m => m.ServiceName == "GetCurrLeaveHospNum" && m.SearchDate == ParmeDt.AddDays(-1)).ToFirstDefault();
            if (getCurrLeaveHospNum != null)
            {
                CurrLeaveHospNum = getCurrLeaveHospNum.ServiceData;
            }

            //今日在院退院人数
            //object getCurrInHospNums = WebServiceHelper.InvokeWebService(url, "GetCurrInHospNum", arge);
            //if (getCurrInHospNums != null)
            //{
            //    CurrInHospNums = getCurrInHospNums.ToString();
            //}
            var getCurrInHospNums = SqlHelper.Context.From<WebServiceData>().Select(m => m.ServiceData)
                   .Where(m => m.ServiceName == "GetCurrInHospNum" && m.SearchDate == ParmeDt).ToFirstDefault();
            if (getCurrInHospNums != null)
            {
                CurrInHospNums = getCurrInHospNums.ServiceData;
            }

            //昨日危重患者人数
            var getCurrCriticallyIllInHospNum = SqlHelper.Context.From<WebServiceData>().Select(m => m.ServiceData)
                   .Where(m => m.ServiceName == "GetCurrCriticallyIllInHospNum" && m.SearchDate == ParmeDt.AddDays(-1)).ToFirstDefault();
            if (getCurrCriticallyIllInHospNum != null)
            {
                CurrCriticallyIllInHospNum = getCurrCriticallyIllInHospNum.ServiceData;
            }

            //昨日业务收入
            var getYesterdayAmount = SqlHelper.Context.From<WebServiceData>().Select(m => m.ServiceData)
                   .Where(m => m.ServiceName == "GetYesterdayAmount" && m.SearchDate == ParmeDt.AddDays(-1)).ToFirstDefault();
            if (getYesterdayAmount != null)
            {
                YesterdayAmount = getYesterdayAmount.ServiceData;
            }

            //昨日门诊人次
            var getYesterdayOutNum = SqlHelper.Context.From<WebServiceData>().Select(m => m.ServiceData)
                   .Where(m => m.ServiceName == "GetYesterdayOutNum" && m.SearchDate == ParmeDt.AddDays(-1)).ToFirstDefault();
            if (getYesterdayOutNum != null)
            {
                YesterdayOutNum = getYesterdayOutNum.ServiceData;
            }

            //门诊收入权限
            var interfacesModel =
                SqlHelper.Context.From<InterfacesTypes>().Where(d => d.InterfaceName == "GetYesterdayAmount").ToFirstDefault();
            if (interfacesModel != null)
            {
                YesterdayAmountquanxian = activityRebateType.Contains<string>(interfacesModel.Id.ToString());
            }

            //昨日入院权限
            var currDayInHospNum =
                SqlHelper.Context.From<InterfacesTypes>().Where(d => d.InterfaceName == "GetCurrDayInHospNum").ToFirstDefault();
            if (currDayInHospNum != null)
            {
                CurrInHospNumquanxian = activityRebateType.Contains<string>(currDayInHospNum.Id.ToString());
            }

            //出院权限
            var CurrOutHospNumModel =
                SqlHelper.Context.From<InterfacesTypes>().Where(d => d.InterfaceName == "GetCurrOutHospNum").ToFirstDefault();
            if (CurrOutHospNumModel != null)
            {
                CurrOutHospNumquanxian = activityRebateType.Contains<string>(CurrOutHospNumModel.Id.ToString());
            }

            //退院权限
            var CurrLeaveHospNumModel =
                SqlHelper.Context.From<InterfacesTypes>().Where(d => d.InterfaceName == "GetCurrLeaveHospNum").ToFirstDefault();
            if (CurrLeaveHospNumModel != null)
            {
                CurrLeaveHospNumquanxian = activityRebateType.Contains<string>(CurrLeaveHospNumModel.Id.ToString());
            }

            //在院权限
            var CurrInHospModel =
                SqlHelper.Context.From<InterfacesTypes>().Where(d => d.InterfaceName == "GetCurrInHospNum").ToFirstDefault();
            if (CurrInHospModel != null)
            {
                CurrInHospNumsquanxian = activityRebateType.Contains<string>(CurrInHospModel.Id.ToString());
            }

            //病危权限
            var CurrCriticallyIllInHospModel =
                SqlHelper.Context.From<InterfacesTypes>()
                    .Where(d => d.InterfaceName == "GetCurrCriticallyIllInHospNum")
                    .ToFirst();
            if (CurrCriticallyIllInHospModel != null)
            {
                CurrCriticallyIllInHospNumsquanxian =
                    activityRebateType.Contains<string>(CurrCriticallyIllInHospModel.Id.ToString());
            }

            //昨日门诊人次权限
            var YesterdayOutModel =
                SqlHelper.Context.From<InterfacesTypes>().Where(d => d.InterfaceName == "GetYesterdayOutNum").ToFirstDefault();
            if (YesterdayOutModel != null)
            {
                YesterdayOutNumquanxian = activityRebateType.Contains<string>(YesterdayOutModel.Id.ToString());
            }
        }
        catch (Exception ex)
        {


        }


    }

    public string GetAuthOpenId()
    {
        string AppId = CommonHelper.GetAppSettings("AppID");
        string AppSecret = CommonHelper.GetAppSettings("AppSecret");
        string strOpenId = "";
        string name = "";
        string code = DNTRequest.GetString("CODE");

        var cookies = HttpContext.Current.Request.Cookies["openidcookie"];
        if (cookies != null && !string.IsNullOrEmpty(cookies.Value))
        {
            strOpenId = cookies.Value;
        }
        else
        {
            if (!String.IsNullOrEmpty(code))
            {
                string json = RequestHelper.SendGet(String.Format("https://api.weixin.qq.com/sns/oauth2/access_token?appid={0}&secret={1}&code={2}&grant_type=authorization_code", AppId, AppSecret, code));
                if (!String.IsNullOrEmpty(json))
                {
                    try
                    {
                        OAuthConfig oAuthConfig = JSONHelper.FromJson<OAuthConfig>(json);
                        if (oAuthConfig != null)
                        {
                            HttpCookie cookie = new HttpCookie("openidcookie");
                            cookie.Value = oAuthConfig.openid;
                            cookie.Expires = DateTime.Now.AddDays(2);
                            HttpContext.Current.Response.Cookies.Add(cookie);
                            strOpenId = oAuthConfig.openid;
                        }

                        else
                        {
                            strOpenId = "";
                        }
                    }
                    catch (Exception ex)
                    {
                        strOpenId = "";
                    }
                }
            }
            else
            {
                var url = string.Format("https://open.weixin.qq.com/connect/oauth2/authorize?appid={0}&redirect_uri={1}%2FPage%2FAutoUpdate%2FWeChatStatics.aspx&response_type=code&scope=snsapi_base&state=test#wechat_redirect", AppId, CommonHelper.GetAppSettings("mainurl").Replace(@"://", "%3A%2F%2F"));
                Response.Redirect(url);

            }
        }
        return strOpenId;
    }

    public void ResponseToPage(string message)
    {
        Response.Redirect(String.Format("/page/Common/message.aspx?m={0}", Server.UrlEncode(message)));
    }
}